package cn.com.connected.service.impl;

import cn.com.connected.common.emnus.StatusType;
import org.springframework.stereotype.Service;
import cn.com.connected.service.LiveMessageService;
import static cn.com.connected.entity.LiveMessageEntity.*;
import cn.com.connected.base.params.page.BaseSearchDto;
import cn.com.connected.base.params.page.PageParam;
import cn.com.connected.base.params.page.PageResult;
import cn.com.connected.base.utils.BeanCopyUtils;
import cn.com.connected.dto.LiveMessageDto;
import cn.com.connected.dto.search.LiveMessageSearchDto;
import cn.com.connected.entity.LiveMessageEntity;
import cn.com.connected.mapper.LiveMessageMapper;
import cn.com.connected.vo.LiveMessageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 意见反馈
 *
 * @author sms
 * @email sms@live.com
 * @date 2022-10-04 20:57:24
 */
@Slf4j
@Service("liveMessageService")
public class LiveMessageServiceImpl implements LiveMessageService {
    @Resource
    private LiveMessageMapper liveMessageMapper;

    @Override
    public LiveMessageVo liveMessageAdd(LiveMessageDto liveMessageDto) {
        LiveMessageEntity entity = new LiveMessageEntity();
        BeanCopyUtils.copy(liveMessageDto,entity);
        liveMessageMapper.insert(entity);

        return liveMessageInfo(entity.getId());
    }

    @Override
    public void liveMessageEdit(String id, LiveMessageDto liveMessageDto) {
        LiveMessageEntity entity = liveMessageMapper.selectById(id);
        BeanCopyUtils.copy(liveMessageDto,entity);
        liveMessageMapper.updateById(entity);
    }

    @Override
    public LiveMessageVo liveMessageInfo(String id) {
        LiveMessageEntity entity = liveMessageMapper.selectById(id);
        return BeanCopyUtils.copy(entity,new LiveMessageVo());
    }

    @Override
    public void liveMessageDel(String id) {
        LiveMessageEntity entity = liveMessageMapper.selectById(id);
        entity.setStatus(StatusType.t0.getValue());
        liveMessageMapper.updateById(entity);
    }

    @Override
    public PageResult<LiveMessageVo> liveMessagePage(BaseSearchDto<LiveMessageSearchDto> searchParams) {
        QueryWrapper<LiveMessageEntity> wrapper = Wrappers.query();
        wrapper.eq(col_status,StatusType.t1.getValue());

        PageParam pageParam = searchParams.getPageParam();
        PageHelper.startPage(pageParam.getPageIndex(), pageParam.getPageSize());
        LiveMessageSearchDto searchDto = searchParams.getSearchOption();

        wrapper.orderByDesc(Arrays.asList(col_create_time));

        Page<LiveMessageEntity> page = (Page)liveMessageMapper.selectList(wrapper);

        PageResult<LiveMessageVo> pageResult = BeanCopyUtils.copyPageResult(page, LiveMessageVo.class);

        return pageResult;
    }

}
